#!/bin/sh

if [ ! -e games.sqlite ]; then
    for f in *.pgn; do pgn2db $f games.sqlite; done
fi

echo "SELECT ifnull(Event,'<NULL>') AS E FROM Games GROUP BY E;"        | sqlite3 -echo -batch games.sqlite >${1}test1.txt; echo "----" >>${1}test1.txt
echo "SELECT ifnull(Site,'<NULL>') AS S FROM Games GROUP BY S;"         | sqlite3 -echo -batch games.sqlite >>${1}test1.txt; echo "----" >>${1}test1.txt
echo "SELECT ifnull(EventDate,'<NULL>') AS ED FROM Games GROUP BY ED;"  | sqlite3 -echo -batch games.sqlite >>${1}test1.txt; echo "----" >>${1}test1.txt
echo "SELECT ifnull(TimeControl,'<NULL>') AS T FROM Games GROUP BY T;"  | sqlite3 -echo -batch games.sqlite >>${1}test1.txt; echo "----" >>${1}test1.txt
echo "SELECT count(*) from Games WHERE length(game) < 10;"              | sqlite3 -echo -batch games.sqlite >>${1}test1.txt; echo "----" >>${1}test1.txt
echo "SELECT count(*) from Games WHERE ECO ISNULL OR ECO='?';"          | sqlite3 -echo -batch games.sqlite >>${1}test1.txt; echo "----" >>${1}test1.txt

echo "SELECT Date,Round,Board FROM Games GROUP BY Date,Round,Board;"    | sqlite3 -echo -batch games.sqlite >${1}test2.txt

echo "SELECT White,ifnull(WhiteElo,'<NULL>') AS WE,ifnull(WhiteTitle,'<NULL>') AS WT FROM Games GROUP BY White,WE,WT;" | sqlite3 -echo -batch games.sqlite >${1}test3.txt; echo "----" >>${1}test3.txt
echo "SELECT Black,ifnull(BlackElo,'<NULL>') AS BE,ifnull(BlackTitle,'<NULL>') AS BT FROM Games GROUP BY Black,BE,BT;" | sqlite3 -echo -batch games.sqlite >>${1}test3.txt; echo "----" >>${1}test3.txt
